﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类joins。
    /// </summary>
    public class joins
    {
        public joins()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_joins where id=" + id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 返回最大序号
        /// </summary>
        /// <returns></returns>
        public int MaxPx()
        {
            string sql = "SELECT MAX(px) FROM lt_joins ";
            DataTable maxdt = DataObject.Query(sql).Tables[0];
            if (maxdt.Rows[0][0].ToString() != "")
            {
                return (Int32.Parse(maxdt.Rows[0][0].ToString()) + 1);
            }
            else
            {
                return 1;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.joins model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_joins(");
            strSql.Append("jid,name,link,pic,px");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("" + model.jid + ",");
            strSql.Append("'" + model.name + "',");
            strSql.Append("'" + model.link + "',");
            strSql.Append("'" + model.pic + "',");
            strSql.Append("" + model.px + "");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.joins model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_joins set ");
            strSql.Append("jid=" + model.jid + ",");
            strSql.Append("name='" + model.name + "',");
            strSql.Append("link='" + model.link + "',");
            strSql.Append("pic='" + model.pic + "',");
            strSql.Append("px=" + model.px + "");
            strSql.Append(" where id=" + model.id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 排序上下移
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="type">上移，下移</param>
        public void PxMove(int id, string type)
        {
            StringBuilder strSql = new StringBuilder();
            if (type.Trim() == "down")
            {
                strSql.Append("update lt_joins set px = (select px from lt_joins where [id]=" + id.ToString() + ") where [id] = (select top 1 [id] from lt_joins where px>(select px from lt_joins where [id]=" + id.ToString() + ") order by px asc) ");
                strSql.Append(" update lt_joins set px = px+1 where [id]=" + id.ToString());
            }
            else
            {
                strSql.Append("update lt_joins set px = (select px from lt_joins where id=" + id.ToString() + ") where [id] = (select top 1 [id] from lt_joins where px<(select px from lt_joins where [id]=" + id.ToString() + ") order by px desc) ");
                strSql.Append(" update lt_joins set px = px-1 where [id]=" + id.ToString());
            }
            DataObject.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_joins ");
            strSql.Append(" where id=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteJid(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_joins ");
            strSql.Append(" where jid=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.joins GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [id],[jid],[name],[link],[pic],[px] ");
            strSql.Append(" from lt_joins ");
            strSql.Append(" where id=" + id);
            ltModel.Model.joins model = new ltModel.Model.joins();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.id = id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["jid"].ToString() != "")
                {
                    model.jid = int.Parse(ds.Tables[0].Rows[0]["jid"].ToString());
                }
                model.name = ds.Tables[0].Rows[0]["name"].ToString();
                model.link = ds.Tables[0].Rows[0]["link"].ToString();
                model.pic = ds.Tables[0].Rows[0]["pic"].ToString();
                if (ds.Tables[0].Rows[0]["px"].ToString() != "")
                {
                    model.px = int.Parse(ds.Tables[0].Rows[0]["px"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],[jid],[name],[link],[pic],[px] ");
            strSql.Append(" FROM lt_joins ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得所有数据列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public DataSet GetAllList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],lt_joins.[jid],[name],[link],[pic],lt_joins.[px],[jname],lt_join_sort.[px] as jpx ");
            strSql.Append(" FROM lt_joins INNER JOIN lt_join_sort on lt_joins.jid = lt_join_sort.jid ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by lt_joins.px asc,  jpx asc , lt_joins.jid  asc");
            return DataObject.Query(strSql.ToString());
        }
        #endregion  成员方法
    }
}